/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from 'antd';
import {
  getBlack,
  addBlack,
  delBlack,
  setSync,
  getSync,
  getBlackScanList,
} from '@/services/blacklist';
import { isLegalData } from '@/utils/utils';

export default {
  namespace: 'blacklist',
  state: {
    list: [],
    pagination: {},
    formVisible: false,
    openVisible: false,
    is_sync: null,
  },
  effects: {
    *getBlack({ payload }, { call, put }) {
      const response = yield call(getBlack, payload);
      const { code, data, msg } = response;
      if (code == 0 && data) {
        const { list, total, page } = data;
        yield put({
          type: 'save',
          payload: {
            list,
            pagination: {
              total,
              current: page,
              sizeType: false,
              showQuickJumper: false,
              pageSize: 15,
            },
          },
        });
      } else {
        message.warning(msg);
      }
    },
    *addBlack({ payload }, { call, put }) {
      const response = yield call(addBlack, payload);
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        yield put({
          type: 'save',
          payload: {
            formVisible: false,
          },
        });
        yield put({
          type: 'getBlack',
          payload: {
            page: 1,
          },
        });
      } else {
        message.warn(msg);
      }
    },
    *delBlack({ payload }, { call, put }) {
      const { page, phone, ...rest } = payload || {};
      const response = yield call(delBlack, rest);
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        yield put({
          type: 'getBlack',
          payload: {
            page,
            phone,
          },
        });
      }
    },
    // 设置黑名单同步状态
    *setSync({ payload }, { call, put }) {
      const response = yield call(setSync, payload);
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        yield put({
          type: 'save',
          payload: {
            openVisible: false,
          },
        });
        yield put({
          type: 'getBlack',
          payload: {
            page: 1,
          },
        });
        yield put({
          type: 'getSync',
        });
      } else {
        message.warn(msg);
      }
    },
    // 获取黑名单同步状态
    *getSync({ payload }, { call, put }) {
      const response = yield call(getSync, payload);
      const { code, data } = response;
      if (code == 0) {
        yield put({
          type: 'save',
          payload: {
            is_sync: data,
          },
        });
      }
    },

    // 获取黑名单扫描记录列表
    *getBlackScanList({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(getBlackScanList, payload);
      if (!response) return;
      const { code, data = {}, msg } = response;
      const { list = [], total = 0, page = 1 } = data;
      if (code == 0) {
        __dva_resolve({
          list: isLegalData(list),
          pagination: {
            total,
            current: page,
            pageSize: 15,
            showQuickJumper: false,
            showSizeChanger: false,
          },
        });
      } else {
        __dva_reject(response);
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
